package agent;

import java.util.ArrayList;
import java.util.List;

import server.Customer;
import server.Product;
import server.ProductLot;
import server.Shop;

/**
 * Dieser Agent hilft beim Shoppen.
 * @author reto
 *
 */
public class ShoppingAgent {	
	private static Shop shop;	
	private Customer customer;	
	private CashPointAgent cashPointAgent;
	
	
	
	/**
	 * Falls noch kein Shop besteht, wird einer kreiert (statisch)
	 * Ein neuer (noch namenloser) Customer wird kreiert.
	 */
	public ShoppingAgent()
	{
		if(ShoppingAgent.shop==null)
		{
			ShoppingAgent.shop=new Shop();				
			ShoppingAgent.shop.getStock().addProduct(new Product("A","Buchstabe A",3.5));
			ShoppingAgent.shop.getStock().addProduct(new Product("B","Buchstabe B",2.5));
			ShoppingAgent.shop.getStock().addProduct(new Product("B","Buchstabe B",2.5));
			ShoppingAgent.shop.getStock().addProduct(new Product("C","Buchstabe C",1.5));
			ShoppingAgent.shop.getStock().addProduct(new Product("C","Buchstabe C",1.5));
			ShoppingAgent.shop.getStock().addProduct(new Product("C","Buchstabe C",1.5));			
		}
		this.customer=new Customer();
		this.cashPointAgent=new CashPointAgent(this.customer);		
	}
	
	/**
	 * Gibt den Kunden zurück
	 * @return Kunde
	 */
	public Customer getCustomer() {
		return customer;
	}
	
	/**
	 * @convenience
	 * @see Customer#getName()
	 */
	public String getCustomerName()
	{
		return this.customer.getName();
	}

	/**
	 * @convenience
	 * @see Customer#setName(String)
	 */
	public void setCustomerName(final String name)
	{
		this.customer.setName(name);
	}
	
	/**
	 * Gesamter Inhalt des Shop-Stocks
	 * @return Array von ProductLots
	 */
	public ProductLotAgent[] getProductLotAgentsFromShop()
	{
		final List<ProductLotAgent>availableProductLots=new ArrayList<ProductLotAgent>();
		for(final ProductLot productLot:ShoppingAgent.shop.getStock().getProductLotsAsCollection())		{
			if(productLot.getAmount()>0)
			{
				availableProductLots.add(new ProductLotAgent(productLot,ShoppingAgent.shop.getStock(),this.customer.getShoppingCart()));
			}
		}		
		return availableProductLots.toArray(new ProductLotAgent[0]);
	}
	
	/**
	 * Gesamter Inhalt des Customer-Cart
	 * @return Array von ProductLots
	 */
	public ProductLotAgent[] getProductLotAgentsFromCustomer()
	{
		final List<ProductLotAgent>availableProductLots=new ArrayList<ProductLotAgent>();
		for(final ProductLot productLot:this.customer.getShoppingCart().getProductLotsAsCollection())		{
			if(productLot.getAmount()>0)
			{
				availableProductLots.add(new ProductLotAgent(productLot,this.customer.getShoppingCart(),ShoppingAgent.shop.getStock()));
			}
		}		
		return availableProductLots.toArray(new ProductLotAgent[0]);
	}
	/**
	 * Interner CashPointAgent
	 * @return gibt den CashPointAgent für diesen Kunden zurück.
	 */
	public CashPointAgent getCashPointAgent() {
		return this.cashPointAgent;
	}
}
